﻿using BLL;
using IBLL;
using MODEL;
using Shop.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace Shop.Controllers
{
    public class ProductController : BaseController<Product,IProductBLL>
    {
        public override IProductBLL Bll {
            get {
                return new ProductBLL();
            }
        }
       
        // GET: Product
        [HttpPost]
        public ActionResult Add(ProductVModel vModel) {
            Product product = vModel.Product;
            List<ProductSku> skulist = vModel.Skus;
            List<ProductAttr> attrlist = vModel.Attrs;
            Bll.Add(product, skulist, attrlist);
            return Json(new { state =  true , msg = "添加成功" });
        }
        public ActionResult GetAll(int draw,int pageSize,int pageIndex)
        {
            var list = Bll.Search(pageSize, pageIndex, false, x=>true);
            var count = Bll.GetCount(x => true);

            var result = new { draw = draw,
                data = list,
                recordsTotal = count,
                recordsFiltered = count
            };
            return Json(result);
            
        }
       [HttpPost]
        public ActionResult Update(ProductVModel vModel)
        {
            Product product = vModel.Product;
            List<ProductSku> skuList = vModel.Skus;
            List<ProductAttr> AttrList = vModel.Attrs;

            int result = Bll.Update(product, skuList, AttrList);

            return Json(new { state = result > 0 });
        }
        public virtual ActionResult Getone(int id) { 
            List<ProductAttr> attrs;
            List<ProductSku> skus;
            var product = Bll.GetOne(id, out attrs, out skus);
            var result = new
            {
                product = product,
                attrs = attrs,
                skus = skus
            };
            return Json(result,JsonRequestBehavior.AllowGet);
            
        }

}
}